package com.example.oppotest.controller;

import com.example.oppotest.entity.User;
import com.example.oppotest.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wyk
 * @Date: 2025/2/15 20:33
 */

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService ;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap register(@RequestBody User user) {
        ModelMap map = new ModelMap();
        try {
            if (userService.insertUser(user) > 0) {
                sendSuccessMessage(map, "注册成功，返回首页登录~");
            } else {
                sendFailureMessage(map, "注册失败，系统异常");
            }
        } catch (Exception e) {
            sendFailureMessage(map, "系统异常");
        }
        return map;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap login(@RequestBody User user, HttpServletRequest request) {
        ModelMap map = new ModelMap();
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("mobileNo", user.getMobileNo());
            paramMap.put("passWord", user.getPassWord());
            User loginUser = userService.selectUser(paramMap);
            if (loginUser == null) {
                sendFailureMessage(map, "该手机号未注册或密码错误");
            } else {
                request.getSession().setAttribute("mobileNo", loginUser.getMobileNo());
                map.put("mobileNo", loginUser.getMobileNo());
                logger.info("用户手机号" + loginUser.getMobileNo() + "登录了系统~");
                sendSuccessMessage(map, "成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMessage(map, "系统异常");
        }
        return map;
    }

    @RequestMapping(value = "/queryUser", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap queryUser(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        HttpSession session = request.getSession();
        String mobileNo = (String) session.getAttribute("mobileNo");
        User loginUser = null;
        if (StringUtils.hasText(mobileNo)) {
            loginUser = userService.selectUserByPhone(mobileNo);
            if (loginUser != null) { // 判断用户状态
                sendSuccessMessage(map, "成功");
            } else {
                session.removeAttribute("mobileNo");
                sendFailureMessage(map, "获取用户信息失败1");
            }
        } else {
            sendFailureMessage(map, "获取用户信息失败2");
        }
        map.put("user", loginUser) ;
        return map;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public ModelMap logout(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        try {
            String mobileNo = (String) request.getSession().getAttribute("mobileNo");
            logger.info("用户手机号" + mobileNo + "退出了系统~");
            request.getSession().removeAttribute("mobileNo");
            sendSuccessMessage(map, "退出登录成功");
        } catch (Exception e) {
            sendFailureMessage(map, "系统异常");
        }
        return map;
    }


}
